<?php

use \Orm\BaseModels;

class FundrecordModel extends BaseModels
{
    /**
     * 默认条件
     */
    private $defalut_where = ['hidden' => 0];

    /**
     * 默认字段
     */
    private $default_field = 'id,orderNo,userId,fundMode,handleSum,usableSum,recordTime,operateType,remarks,operateType as type_words,operateType as type';



    /**
     *  获取单条记录
     * @throws
     */
    public function getOneById($id, $field = '')
    {
        $field = empty($field) ? $this->default_field : $field;
        $dataOne = $this->where($this->defalut_where)->field($field)->find($id);
        return empty($dataOne) ? $dataOne : $dataOne->toArray();

    }

    /**
     * 获取某个用户单条记录(限制当前用户只能查询自己的记录)
     */
    public function getOneRecord($id, $user_id, $field = ''){
        $field = empty($field) ? $this->default_field : $field;
        $where = $this->defalut_where;
        $where['id']        = $id;
        $where['userId']    = $user_id;
        $dataOne = $this->where($where)->field($field)->find();
        return empty($dataOne) ? $dataOne : $dataOne->toArray();
    }

    /************ 获取器 ************/
    //operateType字段值转换为大分类值
    public function getTypeWordsAttr($value)
    {
        $type = [
            '100' => '充值',
            '110' => '提现',
            '111' => '提现',
            '210' => '收回',
            '211' => '收回',
            '501' => '收回',
            '200' => '交易',
            '201' => '交易',
            '202' => '交易',
            '300' => '交易',
            '301' => '交易',
            '303' => '返还',
            '304' => '返还',
            '305' => '返还',
            '306' => '返还',
            '401' => '付款',
            '402' => '完成',
            '403' => '完成',
            '404' => '完成',
        ];
        //NULL不是错误，是被允许的，视为该operateType值没有所属的大分类
        return array_key_exists($value, $type) ? $type[$value] : '其他';
    }

    //operateType字段值转换为大分类代码值
    public function getTypeAttr($value)
    {
        $type = [
            '100' => '1',
            '110' => '2',
            '111' => '2',
            '210' => '3',
            '211' => '3',
            '501' => '3',
            '200' => '4',
            '201' => '4',
            '202' => '4',
            '300' => '5',
            '301' => '5',
            '303' => '6',
            '304' => '6',
            '305' => '6',
            '306' => '6',
            '401' => '7',
            '402' => '8',
            '403' => '8',
            '404' => '8',
        ];

        //-1不是错误，是被允许的，视为该operateType值没有所属的大分类
        return array_key_exists($value, $type) ? $type[$value] : '-1';
    }

}
